package tcu.iot.gasmanage.service;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import tcu.iot.gasmanage.common.Result;
import tcu.iot.gasmanage.entity.export.ExportDataReturn;
import tcu.iot.gasmanage.entity.export.ExportQuery;
import tcu.iot.gasmanage.entity.export.TDActionData;
import tcu.iot.gasmanage.mapper.EquipmentMapper;
import tcu.iot.gasmanage.mapper.ExportMapper;
import tcu.iot.gasmanage.mapper.PersonMapper;
import tcu.iot.gasmanage.mapper.TDengineMapper;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class ExportService {

    @Resource
    private EquipmentMapper equipmentMapper;
    @Resource
    private PersonMapper personMapper;
    @Resource
    private ExportMapper exportMapper;

    public Result getExportService() {
        HashMap<String, Object> hashMap = new HashMap();

        hashMap.put("provinceOptions", equipmentMapper.FindProvinceOptions());
        hashMap.put("regionOptions", personMapper.FindRegionOptions());
        hashMap.put("pipelineOptions", personMapper.FindPipelineOptions());
        hashMap.put("managementOptions", personMapper.FindManagementOptions());
        hashMap.put("principalOptions", personMapper.FindPrincipalOptions());
        hashMap.put("siteOptions", personMapper.FindSiteOptions());

        return Result.ok("true", 200, "true", hashMap);
    }

    public Result getList(ExportQuery exportQuery) {

        List<TDActionData> tdActionDataList = TDengineMapper.ExportActionData(exportMapper.SelectEquipmentList(exportQuery));

        PageHelper.startPage(exportQuery.getPage(),exportQuery.getAmount());

        List<ExportDataReturn> exportDataReturnList = exportMapper.SelectEquipmentData(exportQuery);

        HashMap<String, Object> hashMap = new HashMap();

        for (ExportDataReturn exportDataReturn : exportDataReturnList) {
            for (TDActionData tdActionData : tdActionDataList) {
                if (exportDataReturn.getEquipmentNumber().equals(tdActionData.getEquipmentNumber())) {
                    exportDataReturn.setTime(tdActionData.getTs());
                    exportDataReturn.setI1(tdActionData.getI1());
                    exportDataReturn.setI2(tdActionData.getI2());
                    exportDataReturn.setI3(tdActionData.getI3());
                    exportDataReturn.setI4(tdActionData.getI4());
                    break;
                }
            }
        }

        hashMap.put("equipmentData", exportDataReturnList);
        hashMap.put("total", exportMapper.SelectEquipmentDataCount(exportQuery));
        return Result.ok("true", 200, "成功", hashMap);

    }

}
